package com.smart.controller;

import com.smart.common.ResultVo;
import com.smart.entity.LoginUser;
import com.smart.entity.Toke;
import com.smart.entity.Users;
import com.smart.service.LoginService;
import com.smart.service.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private WechatService wechatService;

    @GetMapping("/user/login")
    public ResultVo login(Users users) {
//        ResponseResult login = userService.Login(username, password);
        Map<String, Object> result = loginService.login(users);
        return ResultVo.ok().data("item", result);
    }

    @RequestMapping("/user/a")
    public ResultVo bbb(@RequestBody Users user) {
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return ResultVo.ok().data("item", "0");
    }

    @PostMapping("/user/vx/login")
    public ResultVo WechatLogin(String code) {
        Map<String, Object> result = wechatService.WechatLogin(code);
        return ResultVo.ok().data("item", result);
    }
}
